home *** CD-ROM | disk | FTP | other *** search
/ NeXTSTEP 3.3 (Developer)…68k, x86, SPARC, PA-RISC] / NeXTSTEP 3.3 Dev Intel.iso / NextDeveloper / Headers / g++ / DLList.h < prev    next >
C/C++ Source or Header  |  1995-02-06  |  4KB  |  126 lines

  1. // This may look like C code, but it is really -*- C++ -*-
  2. /* 
  3. Copyright (C) 1988 Free Software Foundation
  4.     written by Doug Lea (dl@rocky.oswego.edu)
  5.  
  6. This file is part of the GNU C++ Library.  This library is free
  7. software; you can redistribute it and/or modify it under the terms of
  8. the GNU Library General Public License as published by the Free
  9. Software Foundation; either version 2 of the License, or (at your
  10. option) any later version.  This library is distributed in the hope
  11. that it will be useful, but WITHOUT ANY WARRANTY; without even the
  12. implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
  13. PURPOSE.  See the GNU Library General Public License for more details.
  14. You should have received a copy of the GNU Library General Public
  15. License along with this library; if not, write to the Free Software
  16. Foundation, 675 Mass Ave, Cambridge, MA 02139, USA.
  17. */
  18.  
  19.  
  20. #ifndef _DLList_h
  21. #ifdef __GNUG__
  22. //#pragma interface
  23. #pragma cplusplus
  24. #endif
  25. #define _DLList_h 1
  26.  
  27. #include <Pix.h>
  28.  
  29. struct BaseDLNode {
  30.     BaseDLNode *bk;
  31.     BaseDLNode *fd;
  32.     void *item() {return (void*)(this+1);} //Return ((DLNode<T>*)this)->hd
  33. };
  34.  
  35. template<class T>
  36. class DLNode : public BaseDLNode
  37. {
  38.   public:
  39.     T hd;
  40.     DLNode() { }
  41.     DLNode(const T& h, DLNode* p = 0, DLNode* n = 0)
  42.         : hd(h) { bk = p; fd = n; }
  43.     ~DLNode() { }
  44. };
  45.  
  46. class BaseDLList {
  47.   protected:
  48.     BaseDLNode *h;
  49.  
  50.     BaseDLList() { h = 0; }
  51.     void copy(const BaseDLList&);
  52.     BaseDLList& operator= (const BaseDLList& a);
  53.     virtual void delete_node(BaseDLNode*node) = 0;
  54.     virtual BaseDLNode* copy_node(void* datum) = 0;
  55.     virtual void copy_item(void *dst, void *src) = 0;
  56.     virtual ~BaseDLList() { }
  57.  
  58.     Pix                   prepend(void*);
  59.     Pix                   append(void*);
  60.     Pix ins_after(Pix p, void *datum);
  61.     Pix ins_before(Pix p, void *datum);
  62.     void remove_front(void *dst);
  63.     void remove_rear(void *dst);
  64.     void join(BaseDLList&);
  65.  
  66.   public:
  67.     int                   empty() { return h == 0; }
  68.     int                   length();
  69.     void                  clear();
  70.     void                  error(const char* msg);
  71.     int                   owns(Pix p);
  72.     int                   OK();
  73.     void                  del(Pix& p, int dir = 1);
  74.     void                  del_after(Pix& p);
  75.     void                  del_front();
  76.     void                  del_rear();
  77. };
  78.  
  79. template <class T>
  80. class DLList : public BaseDLList {
  81.     //friend class          <T>DLListTrav;
  82.  
  83.     virtual void delete_node(BaseDLNode *node) { delete (DLNode<T>*)node; }
  84.     virtual BaseDLNode* copy_node(void *datum)
  85.     { return new DLNode<T>(*(T*)datum); }
  86.     virtual void copy_item(void *dst, void *src) { *(T*)dst = *(T*)src; }
  87.  
  88.   public:
  89.     DLList() : BaseDLList() { }
  90.     DLList(const DLList<T>& a) : BaseDLList() { copy(a); }
  91.  
  92.     DLList<T>&            operator = (const DLList<T>& a)
  93.     { BaseDLList::operator=((const BaseDLList&) a); return *this; }
  94.     virtual ~DLList() { clear(); }
  95.  
  96.     Pix prepend(T& item) {return BaseDLList::prepend(&item);}
  97.     Pix append(T& item) {return BaseDLList::append(&item);}
  98.  
  99.     void join(DLList<T>& a) { BaseDLList::join(a); }
  100.  
  101.     T& front() {
  102.     if (h == 0) error("front: empty list");
  103.     return ((DLNode<T>*)h)->hd; }
  104.     T& rear() {
  105.     if (h == 0) error("rear: empty list");
  106.     return ((DLNode<T>*)h->bk)->hd;
  107.     }
  108.     T remove_front() { T dst; BaseDLList::remove_front(&dst); return dst; }
  109.     T remove_rear() { T dst; BaseDLList::remove_rear(&dst); return dst; }
  110.  
  111.     T&                  operator () (Pix p) {
  112.     if (p == 0) error("null Pix");
  113.     return ((DLNode<T>*)p)->hd;
  114.     }
  115.     Pix                   first() {  return Pix(h); }
  116.     Pix                   last() { return (h == 0)? 0 : Pix(h->bk); }
  117.     void                  next(Pix& p)
  118.     { p = (p == 0 || p == h->bk)? 0 : Pix(((DLNode<T>*)p)->fd); }
  119.     void                  prev(Pix& p)
  120.     { p = (p == 0 || p == h)? 0 : Pix(((DLNode<T>*)p)->bk); }
  121.     Pix ins_after(Pix p, T& item) {return BaseDLList::ins_after(p, &item); }
  122.     Pix ins_before(Pix p, T& item) {return BaseDLList::ins_before(p, &item);}
  123. };
  124.  
  125. #endif
  126.